/*
 * 文件名：CustomerAction.java
 * 版权：Copyright by www.wootide.com
 * 描述：客户（会员）管理Action
 * 修改人：lizl
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.customer.custmanager.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.wootide.cms.common.Constants;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.customer.custmanager.pojo.ClientLevel;
import com.wootide.cms.service.customer.custmanager.pojo.Customer;
import com.wootide.cms.service.customer.custmanager.service.CustManagerService;
import com.wootide.cms.sys.common.action.BaseAction;

/**
 * 〈一句话功能简述〉客户（会员）管理
 * 〈功能详细描述〉
 * @author lizl
 * @version 2013-2-21
 * @see CustManagerAction
 * @since
 */
public class CustManagerAction extends BaseAction implements
        ServletRequestAware
{
    
    /**
     * 意义，目的和功能，以及被用到的地方<br>
     */
    private static final long serialVersionUID = -1075429494690946732L;
    
    //客户信息
    private Customer customer;
    
    //客户等级 
    private ClientLevel clientLevel;
    
    //日志
    private RunLog runLog;
    
    //用户信息系业务层实例
    private CustManagerService custManagerService;
    
    //分页信息
    private Page page;
    
    //查询条件
    private QueryCondition queryConditon;
    
    // Http请求对象
    private HttpServletRequest request;
    
    // 客户结果集
    @SuppressWarnings("unchecked")
    private List results;
    
    //返回消息
    private String message;
    
    // 是否操作成功
    private Integer actResult = Constants.ACTRESULT_DEF;
    
    // 客户ID
    private Integer innerIndex;
    
    //客户ID集合
    private Integer[] innerIndexs;
    
    /**
     * Description: 跳转到增加界面<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author lizl
     */
    public String toAdd()
    {
        try
        {
            Result result = custManagerService.queryCustomerLevel(queryConditon);
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                QueryResults queryResults = (QueryResults)result.getResultPojo();
                // 获取服务代码结果集
                runLog.debug(queryResults);
                results = queryResults.getResults();
                // 跳转到分页页面
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
            
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * Description: 增加一个新客户<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author lizl
     */
    public String doAdd()
    {
        
        try
        {
            customer.setRegistrationDate(new Timestamp(new Date().getTime()));
            runLog.debug("customer=" + customer);
            
            // 调用业务层进行前转MO路由新增操作
            Result result = custManagerService.addOneCustomer(customer, request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
            
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 增加会员级别
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author zqf
     */
    public String doAddLevel()
    {
        try
        {
            runLog.debug("clientLevel=" + clientLevel);
            
            // 调用业务层进行前转MO路由新增操作
            Result result = custManagerService.addOneClientLevel(clientLevel,
                    request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
            
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 查询会员等级
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author zqf
     */
    public String doQueryLevel()
    {
        try
        {
            // 如果从菜单进入，即在没有分页信息的情况下，则默认进入首页
            if (null == page)
            {
                page = new Page();
                page.setCurPage(1);
            }
            
            // 调用而业务层进行分页查询
            queryConditon = null == queryConditon ? new QueryCondition()
                    : queryConditon;
            queryConditon.setObjClass(ClientLevel.class);
            Result result = custManagerService.queryLevelForPages(page,
                    queryConditon);
            
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                QueryResults queryResults = (QueryResults)result.getResultPojo();
                // 获取服务代码结果集
                runLog.debug(queryResults);
                results = queryResults.getResults();
                // 获取分页信息
                page = queryResults.getPage();
                
                // 跳转到分页页面
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
            
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 查询会员信息
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author zqf
     */
    public String doQuery()
    {
        
        try
        {
            // 如果从菜单进入，即在没有分页信息的情况下，则默认进入首页
            if (null == page)
            {
                page = new Page();
                page.setCurPage(1);
            }
            
            // 调用而业务层进行分页查询
            queryConditon = null == queryConditon ? new QueryCondition()
                    : queryConditon;
            queryConditon.setObjClass(Customer.class);
            Result result = custManagerService.queryForPages(page,
                    queryConditon);
            
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                QueryResults queryResults = (QueryResults)result.getResultPojo();
                // 获取服务代码结果集
                runLog.debug(queryResults);
                results = queryResults.getResults();
                // 获取分页信息
                page = queryResults.getPage();
                
                // 跳转到分页页面
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
            
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 预修改会员信息
     * Implement: <br>
     * @see
     * @return String 跳转标识
     * @author 
     */
    public String toMod()
    {
        try
        {
            Result result = custManagerService.queryCustomerLevel(queryConditon);
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                QueryResults queryResults = (QueryResults)result.getResultPojo();
                // 获取服务代码结果集
                runLog.debug(queryResults);
                results = queryResults.getResults();
                try
                {
                    // 调用业务层获取一个路由对象
                    result = custManagerService.viewOneCustomer(innerIndex);
                    resultCode = result.getResultCode();
                    if (ErrorCodes.ERRORCODE_SUCC == resultCode)
                    {
                        customer = (Customer)result.getResultPojo();
                        return SUCCESS;
                    }
                    else
                    {
                        message = getText(Constants.MESSAGE_PREFIX + resultCode);
                        return ERROR;
                    }
                }
                catch (Exception e)
                {
                    runLog.error("Unknow Error!", e, request);
                    message = getText(Constants.MESSAGE_PREFIX
                            + ErrorCodes.ERRORCODE_UNKNOW);
                    return ERROR;
                }
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
            
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    public String toModLevel()
    {
        try
        {
            // 调用业务层获取一个路由对象
            Result result = custManagerService.viewOneLevel(innerIndex);
            int resultCode = result.getResultCode();
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                clientLevel = (ClientLevel)result.getResultPojo();
                return SUCCESS;
            }
            else
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    public String doMod()
    {
        //接口修改
        try
        {
            // 调用业务层进行路由的修改操作
            Result result = custManagerService.modOneCustomer(customer, request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    public String doModLevel()
    {
        try
        {
            // 调用业务层进行路由的修改操作
            Result result = custManagerService.modOneLevel(clientLevel, request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 客户挂失
     */
    public String doLost()
    {
        //接口修改
        
        try
        {
            // 调用业务层进行路由的修改操作
            Result result = custManagerService.lostOneCustomer(customer,
                    request);
            int resultCode = result.getResultCode();
            message = getText(Constants.MESSAGE_PREFIX + resultCode);
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            else
            {
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 删除客户
     * Implement: <br>
     * @see
     * @return String 跳转标识
     * @author 
     */
    public String doDel()
    {
        try
        {
            runLog.debug("doDel");
            // 调用业务层进行删除操作
            Result result = custManagerService.delCustomers(innerIndexs,
                    request);
            int resultCode = result.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            // 如果删除中出现失败
            else
            {
                // 取得删除失败的条数
                int delErrorNum = result.getDelErrorNum();
                // 根据总条数和失败条数得出成功的条数
                int delSuccNum = innerIndexs.length - delErrorNum;
                // 定义国际化消息的参数
                String[] params = new String[] {String.valueOf(delSuccNum),
                        String.valueOf(delErrorNum)};
                message = getText(Constants.MESSAGE_PREFIX + resultCode, params);
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    public String doDelLevel()
    {
        try
        {
            runLog.debug("doDel");
            // 调用业务层进行删除操作
            Result result = custManagerService.delClientLevels(innerIndexs,
                    request);
            int resultCode = result.getResultCode();
            
            if (ErrorCodes.ERRORCODE_SUCC == resultCode)
            {
                message = getText(Constants.MESSAGE_PREFIX + resultCode);
                actResult = Constants.ACTRESULT_SUC;
                return SUCCESS;
            }
            // 如果删除中出现失败
            else
            {
                // 取得删除失败的条数
                int delErrorNum = result.getDelErrorNum();
                // 根据总条数和失败条数得出成功的条数
                int delSuccNum = innerIndexs.length - delErrorNum;
                // 定义国际化消息的参数
                String[] params = new String[] {String.valueOf(delSuccNum),
                        String.valueOf(delErrorNum)};
                message = getText(Constants.MESSAGE_PREFIX + resultCode, params);
                actResult = Constants.ACTRESULT_FAIL;
                return ERROR;
            }
        }
        catch (Exception e)
        {
            runLog.error("Unknow Error!", e, request);
            actResult = Constants.ACTRESULT_FAIL;
            message = getText(Constants.MESSAGE_PREFIX
                    + ErrorCodes.ERRORCODE_UNKNOW);
            return ERROR;
        }
    }
    
    /**
     * 
     * Description: 跳转会员管理首页
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author zqf
     */
    public String index()
    {
        return SUCCESS;
    }
    
    /**
     * 
     * Description: 跳转到增加会员级别页面
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author wootide-0005
     */
    public String toAddLevel(){
        return SUCCESS;
    }
    
    @Override
    public void setServletRequest(HttpServletRequest request)
    {
        this.request = request;
        
    }
    
    public Customer getCustomer()
    {
        return customer;
    }
    
    public void setCustomer(Customer customer)
    {
        this.customer = customer;
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
    public CustManagerService getCustManagerService()
    {
        return custManagerService;
    }
    
    public void setCustManagerService(CustManagerService custManagerService)
    {
        this.custManagerService = custManagerService;
    }
    
    public Page getPage()
    {
        return page;
    }
    
    public void setPage(Page page)
    {
        this.page = page;
    }
    
    public QueryCondition getQueryConditon()
    {
        return queryConditon;
    }
    
    public void setQueryConditon(QueryCondition queryConditon)
    {
        this.queryConditon = queryConditon;
    }
    
    public HttpServletRequest getRequest()
    {
        return request;
    }
    
    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }
    
    @SuppressWarnings("unchecked")
    public List getResults()
    {
        return results;
    }
    
    @SuppressWarnings("unchecked")
    public void setResults(List results)
    {
        this.results = results;
    }
    
    public String getMessage()
    {
        return message;
    }
    
    public void setMessage(String message)
    {
        this.message = message;
    }
    
    public Integer getActResult()
    {
        return actResult;
    }
    
    public void setActResult(Integer actResult)
    {
        this.actResult = actResult;
    }
    
    public Integer getInnerIndex()
    {
        return innerIndex;
    }
    
    public void setInnerIndex(Integer innerIndex)
    {
        this.innerIndex = innerIndex;
    }
    
    public Integer[] getInnerIndexs()
    {
        return innerIndexs;
    }
    
    public void setInnerIndexs(Integer[] innerIndexs)
    {
        this.innerIndexs = innerIndexs;
    }
    
    public ClientLevel getClientLevel()
    {
        return clientLevel;
    }
    
    public void setClientLevel(ClientLevel clientLevel)
    {
        this.clientLevel = clientLevel;
    }
    
}
